const lyricReg = /\[(\d{2}):(\d{2})\.(\d{2,3})\]/

export default function parseLyric(lyricString) {
  const lyricStrings = lyricString.split('\n')
  const lyricList = []
  for (const lineString of lyricStrings) {
    const lyricInfo = lyricReg.exec(lineString)
    if (!lyricInfo) {
      const prevTime = lyricList[lyricList.length - 1].time * 10
      lyricList.push({ time: prevTime, text: '' })
      continue
    }
    // 获取歌词时间
    const minute = lyricInfo[1] * 60 * 1000
    const second = lyricInfo[2] * 1000
    const millsecond = lyricInfo[3].length === 2 ? lyricInfo[3] * 10 : lyricInfo[3] * 1
    const time = minute + second + millsecond

    // 获取歌词文本
    const text = lineString.replace(lyricReg, '')

    lyricList.push({time, text})
  }
  return lyricList
}